package psn.kiko.controller.inter_face;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import psn.kiko.annotation.api.StableID;
import psn.kiko.constant.MessageConstant;
import psn.kiko.constant.ResponseCodeConstant;
import psn.kiko.pojo.PageResult;
import psn.kiko.pojo.Result;
import psn.kiko.pojo.dto.InterfaceDTO;
import psn.kiko.pojo.vo.DelDetailVO;
import psn.kiko.pojo.vo.LabelValue;
import psn.kiko.pojo.vo.PureInterfaceVO;
import psn.kiko.pojo.vo.ServerPhysicFileVO;
import psn.kiko.service.business.system.FileService;
import psn.kiko.service.business.system.InterfaceService;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * api接口管理控制器
 */
@RestController
@RequestMapping("/admin/interface")
@Api(tags = {"API管理"})
public class InterfaceController{
	@Autowired
	FileService fileService;
	@Autowired
	InterfaceService interfaceManagementService;
	
	@PostMapping("/pagingGetInterfaces/{pageNum}/{pageSize}")
	@ApiOperation("携带条件地分页获取接口")
	@StableID(401)
	public Result pagingGetInterfaces(
			@RequestBody(required = false) @ApiParam(value = "接口查询条件DTO", required = false)InterfaceDTO interfaceDTO,
			@PathVariable("pageNum") @ApiParam(value = "页码", required = true) int pageNum,
			@PathVariable("pageSize") @ApiParam(value = "每页大小", required = true)  int pageSize
	){
		PageResult pageResult = interfaceManagementService.getInterfaceEntities(interfaceDTO,pageNum,pageSize);
		return Result.success(pageResult);
	}
	
	@GetMapping("/childrenOfDir")
	@ApiOperation("获取指定目录的子文件信息")
	@StableID(402)
	public Result<?> childrenOfDir(
			@ApiParam(value = "目录的绝对路径", required = true)
			@RequestParam("dirAbsolutePath")
					String dirAbsolutePath
	) throws FileNotFoundException{
		List<ServerPhysicFileVO> serverPhysicFileVOS = fileService.childreOfDirLTController(dirAbsolutePath);
		return Result.success(serverPhysicFileVOS);
	}
	
	@GetMapping("/returnParentDirOfCurDir")
	@ApiOperation("返回指定文件所在目录的父目录的子文件信息")
	@StableID(403)
	public Result<?> returnParentDirOfCurDir(
			@ApiParam(value = "指定的文件", required = true)
			@RequestParam("specificFile")
					String specificFile
	) throws FileNotFoundException{
		List<ServerPhysicFileVO> fileVOS = fileService.returnParentDirOfCurDirLTController(specificFile);
		return Result.success(fileVOS);
	}
	
	// 可访问的资源根路径为classpath对应的文件目录
	@GetMapping("/visibilityOfResourceRoot")
	@ApiOperation("获取Controller文件所在的根路径")
	@StableID(404)
	public Result<Object> getResouceRootDirectory() throws FileNotFoundException{
		String classPath = ResourceUtils.getURL("classpath:").getPath();
		String controllerPath = "psn/kiko/controller";
		String rootPath = new File(classPath,controllerPath).getAbsolutePath();
		Result<Object> result = Result.builder()
				.code(ResponseCodeConstant.BUSINESS_OPERATION_SUCCESS)
				.msg(MessageConstant.BUSINESS_OPERATION_SUCCESS)
				.data(rootPath).build();
		return result;
	}
	
	@PostMapping("/parseControllerFile")
	@ApiOperation("解析Controller接口入库")
	@StableID(405)
	public Result<Object> parseControllerFile(
			@ApiParam(value = "指定的Controller文件", required = true)
			@RequestParam("controllerFile")
					String controllerFile
	){
		Integer result = interfaceManagementService.parseControllerFile(controllerFile);
		return Result.builder()
				.code(ResponseCodeConstant.BUSINESS_OPERATION_SUCCESS)
				.msg(MessageConstant.FILE_PARSE_SUCCESS)
				.data(result)
				.build();
	}
	
	@GetMapping("/apiGroups")
	@ApiOperation("获取所有接口所属分组")
	@StableID(406)
	public Result getApiGroups(){
		Set<String> apiGroups = interfaceManagementService.listApiGroups();
		return Result.success(apiGroups);
	}
	
	@GetMapping("/apiClassNames")
	@ApiOperation("获取所有接口所属类")
	@StableID(407)
	public Result getApiClassNames(){
		Set<String> apiClassNames = interfaceManagementService.listApiClassNames();
		return Result.success(apiClassNames);
	}
	
	@GetMapping("/apiRequestMethods")
	@ApiOperation("获取请求方式所有可选值")
	@StableID(408)
	public Result getApiRequestMethods(){
		Set<String> requestMethods = interfaceManagementService.listApiRequestMethods();
		return Result.success(requestMethods);
	}
	
	@GetMapping("/apiStatuses")
	@ApiOperation("获取启用状态所有可选值")
	@StableID(409)
	public Result<?> getApiEnables(){
		List<LabelValue> statuses = interfaceManagementService.listApiStatuses();
		return Result.success(statuses);
	}
	
	@DeleteMapping("/batchDeleteApis")
	@ApiOperation("根据主键批量删除接口")
	@StableID(410)
	public Result batchDeleteApis(
			@RequestBody @ApiParam(value = "接口id集合", required = true) List<Integer> ids){
		ArrayList<DelDetailVO> delDetailVOS = interfaceManagementService.batchDeleteApiInfoByIds(ids);
		return Result.success(delDetailVOS);
	}
	
	@PutMapping("batchEnableByIds")
	@ApiOperation("根据主键批量启用接口")
	@StableID(411)
	public Result batchEnableByIds(@RequestBody @ApiParam("接口id集合") List<Integer> ids){
		Integer r = interfaceManagementService.batchEnableApisByIds(ids);
		return Result.success(r);
	}
	
	@PutMapping("batchDisableByIds")
	@ApiOperation("根据主键批量禁用接口")
	@StableID(412)
	public Result batchDisableByIds(@RequestBody @ApiParam("接口id集合") List<Integer> ids){
		Integer r = interfaceManagementService.batchDisableApisByIds(ids);
		return Result.success(r);
	}
	
	@PutMapping("/updateInterface")
	@ApiOperation("编辑接口")
	@StableID(413)
	public Result updateInterface(@RequestBody @ApiParam("接口DTO") InterfaceDTO interfaceDTO){
		Integer r = interfaceManagementService.updateInterface(interfaceDTO);
		return Result.success(r);
	}
	
	@GetMapping("/interface/{id}")
	@ApiOperation("根据主键获取接口")
	@StableID(414)
	public Result<?> getInterface(@PathVariable("id") @ApiParam("接口DTO") Integer id){
		PureInterfaceVO inteface = interfaceManagementService.getIntefaceById(id);
		return Result.success(inteface);
	}
}
